package com.sunjy.demo1.common;

import com.sunjy.common.domain.ApiResponseT;
import com.sunjy.common.domain.HeaderEnum;
import com.sunjy.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @Auther: sunjy
 * @Date: 2018/10/18 22:07
 * @Description:
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLException.class)
    @ResponseBody
    public ApiResponseT handleSQLException(HttpServletRequest request, Exception ex) {
        log.error("sql执行异常->{}",ex);
        return ApiResponseT.error(HeaderEnum.SERVICE_MSG);
    }

   /* @ExceptionHandler(IOException.class)
    @ResponseBody
    public void handleIOException(){

        return ApiResponseT.error(HeaderEnum.SERVICE_MSG);
    }*/

    @ResponseBody
    @ExceptionHandler(ServiceException.class)
    public ApiResponseT seerviceException(ServiceException ex) {
        log.error("业务执行异常->{}",ex);
        return ApiResponseT.error(ex.getErrorCode(),ex.getErrorMessage());
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ApiResponseT seerviceException(Exception ex) {
        log.error("后台服务执行异常->{}",ex);
        return ApiResponseT.error(HeaderEnum.SYS_MSG);
    }
}
